package com.chineseall.rebirth.bridge.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import jmind.core.util.DataUtil;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chineseall.rebirth.api.pojo.Useraccount;
import com.chineseall.rebirth.bridge.pojo.Book;
import com.chineseall.rebirth.bridge.zedis.ZedisAdapter;
import com.chineseall.rebirth.bridge.zedis.ZedisConsts;
import com.google.common.collect.Maps;

@Service
public class ZedisService {

    // @MethodMonitor(args = true)
    public Book getBook(int bookId) {
        return ZedisAdapter.get(ZedisConsts.ENV_BOOK, ZedisConsts.GET_BOOK + bookId, Book.class);
    }

    public String getBookName(Integer bookId) {
        String string = ZedisAdapter.get(ZedisConsts.ENV_BOOK, ZedisConsts.GET_BOOKNAME + bookId);
        JSONObject jo = JSON.parseObject(string);
        return jo.getString(bookId.toString());
    }

    public Book getBookByName(String name) {
        List<Book> list = ZedisAdapter.getList(ZedisConsts.ENV_BOOK, ZedisConsts.GET_BOOK_BY_NAME + name, Book.class);
        if (list.isEmpty())
            return null;
        return list.get(0);
    }

    @SuppressWarnings("unchecked")
    public Map<Integer, String> getBookNames(Collection<Integer> bookId) {
        if (DataUtil.isEmpty(bookId))
            return Maps.newHashMap();
        String string = ZedisAdapter.get(ZedisConsts.ENV_BOOK, ZedisConsts.GET_BOOKNAME + DataUtil.join(bookId, ","));
        return JSON.parseObject(string, Map.class);

    }

    public Useraccount getAccount(int cid) {
        return ZedisAdapter.get(ZedisConsts.ENV_ACCOUNT, ZedisConsts.GET_ACCOUNT + cid, Useraccount.class);
    }

}
